﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using Anay.TaskScheduling.Common;

namespace Anay.TaskScheduling
{
    public partial class TaskScheduling : Form
    {
        private IConfigHelper ConfigHelper = new ConfigFactory().GetInstance();
        private TaskScheduingHepler taskScheduingHepler = new TaskScheduingHepler();

        public TaskScheduling()
        {
            InitializeComponent();
            MeStartHelper.SetMeStart(true);//开机自启动
        }

        /// <summary>
        /// 刷新列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            var TaskList = ConfigHelper.GetTaskConfigList();
            if (TaskList.Count > 0)
            {
                foreach (var item in TaskList)
                {
                    comboBox1.Items.Add(item.JobName);
                }
                comboBox1.SelectedIndex = 0;
                var config = ConfigHelper.GetTask_ConfigByJobName(comboBox1.SelectedItem.ToString());
                label3.Text = config != null ? (ConfigHelper.GetLastRunTime(config.Id) ?? DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss") : "数据库找不到该JOB";
            }
        }

        /// <summary>
        /// 任务重新启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            QuartzHelper quartzHelper = new QuartzHelper();
            quartzHelper.PauseAll();//暂停任务
            quartzHelper.DeleteJobs();//移除所有任务
            taskScheduingHepler.TaskScheduingRun().GetAwaiter().GetResult();//重新开始
            quartzHelper.Start();//启动
            quartzHelper.ResumeAll();//恢复任务调度状态
            button1_Click(null, null);
            MessageBox.Show(@"重新启动成功");
        }

        /// <summary>
        /// 下拉框重新选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var JobName = comboBox1.Items[comboBox1.SelectedIndex].ToString();
            var config = ConfigHelper.GetTask_ConfigByJobName(comboBox1.SelectedItem.ToString());
            label3.Text = config != null ? (ConfigHelper.GetLastRunTime(config.Id) ?? DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss") : "数据库找不到该JOB";
        }

        private void TaskScheduling_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Environment.Exit(System.Environment.ExitCode);//退出完并杀死进程
        }

        private void TaskScheduling_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show(@"不允许关闭窗口，若要关闭，请从任务管理器中杀掉进程。");
            e.Cancel = true;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TaskScheduling_Shown(object sender, EventArgs e)
        {
            new Common.AppQuartz().RunProgram().GetAwaiter().GetResult();

            taskScheduingHepler.TaskScheduingRun().GetAwaiter().GetResult();

            var TaskList = ConfigHelper.GetTaskConfigList();
            if (TaskList.Count > 0)
            {
                comboBox1.Items.Clear();
                foreach (var item in TaskList)
                {
                    comboBox1.Items.Add(item.JobName);
                }
                comboBox1.SelectedIndex = 0;
                var config = ConfigHelper.GetTask_ConfigByJobName(comboBox1.SelectedItem.ToString());
                label3.Text = config != null ? (ConfigHelper.GetLastRunTime(config.Id) ?? DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss") : "数据库找不到该JOB";
            }
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            TaskConfig taskConfig = new TaskConfig();
            taskConfig.ShowDialog();
        }

        //关闭程序
        private void Button4_Click(object sender, EventArgs e)
        {
            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
            DialogResult dr=   MessageBox.Show("确认关闭程序?", "关闭程序", messButton);
            if (dr==DialogResult.OK)
            {
                Process.GetCurrentProcess().Kill();
            }
        }
    }
}